package 力扣;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class 两个数组的交集 {
    public static void main(String[] args) {
        int[] nums1={1,2,3};
        int[] nums2={1,2,3};
        int[] ints = intersection(nums1, nums2);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }
    public static int[] intersection(int[] nums1, int[] nums2) {
        List<Integer> num = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])){
                num.add(nums2[i]);
                set.remove(nums2[i]);
            }
        }
        int i=0;
        int[] nums=new int[num.size()];
        for (Integer m:num){
            nums[i++]=m;
        }
        return nums;
    }
}
